package tongwentongshu.com.app.activity;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Map;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.PersonalBean;
import tongwentongshu.com.app.bean.TextEvent;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/21.
 */

public class PublicTextActivity extends BaseActivity {
    @BindView(R.id.et_text)
    EditText text;
    @BindView(R.id.title_complete)
    TextView complete;
    private Map<String, String> map;
    private String type;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_public_text;
    }

    @Override
    protected int getTitleId() {
        return getIntent().getExtras().getInt("textId");
    }

    @Override
    protected void initView() {
        type = getIntent().getExtras().getString("type");
        text.setHint(getIntent().getExtras().getInt("textHint"));
        if (getIntent().getExtras().getString("type").equals("note"))
            text.setText(getIntent().getExtras().getString("text"));
        complete.setText("完成");
        complete.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {
        complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (type) {
                    case "note":
                        String info = text.getText().toString().trim();
                        EventBus.getDefault().post(new TextEvent(info, "", ""));
                        finish();
                        break;
                    case "info":
                    case "nickname":
                        editPerson();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void editPerson() {
        String content = text.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.show(mContext, "不能为空");
            return;
        }
        if (type.equals("nickname")) {
            if (content.length() > 12 || content.length() < 2) {
                ToastUtil.show(mContext, "昵称长度不符合规范");
                return;
            }
        }
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());
        if (type.equals("nickname")) {
            map.put("nickname", text.getText().toString().trim());
        } else {
            map.put("nickname", Cache.getUser().getData().getNickname());
        }
        if (type.equals("info")) {
            map.put("memo", text.getText().toString().trim());
        } else {
            map.put("memo", Cache.getUser().getMemo());
        }
        map.put("province", Cache.getUser().getData().getProvince());
        map.put("sex", Cache.getUser().getData().getSex());
        UpdateFractory.getBuild()
                .name("EditPersonCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem per, String msg) {
                        String info = text.getText().toString().trim();
                        if (type.equals("nickname")) {
                            EventBus.getDefault().post(new TextEvent("", info, ""));
                        } else if (type.equals("info")) {
                            EventBus.getDefault().post(new TextEvent("", "", info));
                        }
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }
}
